package com.carbohydrate42.kotlin_compose_demo.page

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.carbohydrate42.kotlin_compose_demo.config.Router
import com.carbohydrate42.kotlin_compose_demo.module.EasyBottomSheetDialog
import com.carbohydrate42.kotlin_compose_demo.MainState


@Preview(showSystemUi = true, showBackground = true)
@Composable
fun Page1(
    modifier: Modifier = Modifier
) {
    val mainState: MainState = MainState.instance
    val shouldShowGlobalSnackBar by mainState.mainPageSnackBarVisible.collectAsState()
    val nav by mainState.nav.collectAsState()

    var visible1 by remember {
        mutableStateOf(false)
    }

    var visible2 by remember { mutableStateOf(false) }



    Column {
        Text("Page1")

        Button(onClick = { nav?.navigate(Router.SubPage1.route) }) {
            Text("SubPage1")
        }

        Button(onClick = { nav?.navigate(Router.SubPage2.route) }) {
            Text("SubPage2")
        }

        Button(onClick = { nav?.navigate(Router.BlankLayoutPage.route) }) {
            Text("BlankLayoutPage")
        }

        Button(onClick = { mainState.toggleMainPageSnackBarVisible() }) {
            Text(text = "toggleGlobalSnackBarVisible")
        }

        Text(text = "shouldShowGlobalSnackBar : $shouldShowGlobalSnackBar")

        Button(onClick = {
            visible1 = true
        }) {
            Text(text = "ShowDialog1")
        }
        Text(text = "visible1 : $visible1")

        Button(onClick = { visible2 = true }) {
            Text(text = "showDialog2")
        }
        Text(text = "visible2 : $visible2")

    }

    GlobalCoverDialog(visible = visible2, onVisibleChange = { visible2 = it })

    BottomDialog(visible = visible1, onVisibleChange = { visible1 = it })
}


@Composable
private fun GlobalCoverDialog(visible: Boolean, onVisibleChange: (Boolean) -> Unit) {
    if (visible) {
        Dialog(
            onDismissRequest = { },
            properties = DialogProperties(usePlatformDefaultWidth = false)
        ) {
            Box(
                Modifier
                    .fillMaxWidth()
                    .fillMaxHeight()
                    .clickable {
                        onVisibleChange(false)
                    }) {
                Text(text = "显示自己的内容",
                    Modifier
                        .fillMaxWidth()
                        .height(300.dp)
                        .clickable { }
                        .align(Alignment.BottomCenter)
                        .background(Color.Cyan))
            }

        }
    }
}

@Composable
private fun BottomDialog(visible: Boolean, onVisibleChange: (Boolean) -> Unit) {

    EasyBottomSheetDialog(
        modifier = Modifier,
        visible = visible,
        cancelable = true,
        canceledOnTouchOutside = true,
        onDismissRequest = {
            onVisibleChange(false)
        }
    ) {
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight(fraction = 0.7f)
                .clip(shape = RoundedCornerShape(topStart = 24.dp, topEnd = 24.dp))
                .background(color = Color(0xFF009688)),
            verticalArrangement = Arrangement.Center
        ) {
            Button(
                modifier = Modifier
                    .align(alignment = Alignment.CenterHorizontally),
                onClick = {
                    onVisibleChange(false)
                }) {
                Text(
                    modifier = Modifier.padding(all = 4.dp),
                    text = "dismissDialog",
                    fontSize = 16.sp
                )
            }
        }
    }
}



